package authController;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Utilisateur;

/**
 * Servlet implementation class Authentification
 */
@WebServlet("/Authentification")
public class Authentification extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Authentification() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/authentification/formulaireAuth.jsp");
		dispatch.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession _session = request.getSession(true);
		
		//on efface la session utilisateur si celui ci est déja loger
		if(_session.getAttribute("Usersession") != null)
			_session.setAttribute("Usersession", null);
		
		Utilisateur user = new Utilisateur();
		String nom = request.getParameter("nom");
		String prenom = request.getParameter("prenom");
		String strDispatch = "";
		
		user.setNom(nom);
		user.setPrenom(prenom);
		
		try{
			if(user.verifUser()==true){
				System.out.println("utilisateur existant");
				System.out.println("fonction : "+user.getFonction());
				if(user.isEleve()){
					System.out.println("eleve");
					_session.setAttribute("Usersession", user);
					strDispatch = "ServletFrontEleve";
					response.sendRedirect(strDispatch);
					return;
				}
				else if(user.isProf()){
					System.out.println("prof");
					strDispatch = "ServletHomeProf";
					_session.setAttribute("Usersession", user);
					response.sendRedirect(strDispatch);
					return;
				}
				else if(user.isAdmin()){
					System.out.println("admin");
					_session.setAttribute("Usersession", user);
					strDispatch = "ServletHomeAdmin";
					response.sendRedirect(strDispatch);
					return;
				}
				else{
					strDispatch = "Authentification";
					response.sendRedirect(strDispatch);
					return;
				}
				
					
			}
			
			else{
				System.out.println("Mauvais NOM/PRENOM");
				strDispatch = "/authentification/formulaireAuth.jsp";
			}
			
			System.out.println("strDispatch = "+strDispatch);
			
			RequestDispatcher dispatch = getServletContext().getRequestDispatcher(strDispatch);
			dispatch.forward(request, response);
			
			
		} catch (ClassNotFoundException e) {
			System.out.println("CATCH2");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
